package com.android.jcyl.rerijaapps.empleo;

import org.androidannotations.annotations.EActivity;

import android.util.Log;
import android.view.View;

import com.android.jcyl.rerijaapps.empleo.animation.FlipAnimation;
import com.android.jcyl.rerijaapps.empleo.dao.Sync;
import com.android.jcyl.rerijaapps.empleo.dao.dao.SyncDao;
import com.android.jcyl.rerijaapps.empleo.files.FileManager;
import com.android.jcyl.rerijaapps.empleo.storage.Constants;
import com.android.jcyl.rerijaapps.empleo.utils.ToastHelper;

@EActivity( R.layout.principal_activity_layout )
public class PrincipalActivity extends BaseActivity 
{

	private FileManager mFileManager;
	private final String URI = "http://www.datosabiertos.jcyl.es/web/jcyl/risp/es/directorio/oficinas-ecyl/1284304566876.csv";
	private Sync mSync;
	
	@Override
	public void postCreate() 
	{
		SyncDao syncDao = new SyncDao( this );
		if ( 0 == syncDao.listAll().size() )
		{
			mSync = new Sync();
			long idSync = syncDao.insert( mSync );
			Log.i( "INSERT_SYNC", "Sync insertada, ID: " + idSync );
		}
		else
		{
			mSync = syncDao.listAll().get( 0 );
		}
		mFileManager = new FileManager( this );
		mAquery.id( R.id.principal_button_begin ).clicked( this, "clickButtonBegin" );
		mAquery.id( R.id.principal_button_begin_back ).clicked( this, "clickButtonBegin" );
	}
	
	public void clickButtonBegin( View view )
	{
		View front = mAquery.id( R.id.principal_button_begin ).getView();
		View back = mAquery.id( R.id.principal_button_begin_back ).getView();
		FlipAnimation flip = new FlipAnimation( front, back, 500, null, 0 );
		
		if ( R.id.principal_button_begin == view.getId() )
		{
			mAquery.id( R.id.principal_progress_text_begin ).visible().text( mSync.isBeginFinish() ? R.string.enter : R.string.cancel );
			
			if ( !mSync.isBeginFinish() )
			{
				mAquery.id( R.id.principal_button_begin ).enabled( false );
				mAquery.id( R.id.principal_button_begin_back ).enabled( true );
				mAquery.id( R.id.principal_progressbar ).getProgressBar().setIndeterminate( true );
				mFileManager.setCancel( false );
				mFileManager.beginDownloadTask( URI );
			}
			mAquery.id( R.id.principal_cancel ).gone().animate( R.anim.scale_up_out ).enabled( false );
			mAquery.id( R.id.principal_button ).animate( flip );
		}
		else
		{
			mSync = new SyncDao( this ).listAll().get( 0 );
			if ( !mSync.isBeginFinish() )
			{
				mFileManager.setCancel( true );
				mAquery.id( R.id.principal_button_begin ).enabled( true );
				mAquery.id( R.id.principal_button_begin_back ).enabled( false );
				flip.reverse();
				mAquery.id( R.id.principal_progressbar ).getProgressBar().setIndeterminate( false );
				mAquery.id( R.id.principal_cancel ).visible().animate( R.anim.scale_up_in ).enabled( true );
				mAquery.id( R.id.principal_button ).animate( flip );
			}
		}
	}

	@Override
	public void onBackPressed() 
	{
		moveTaskToBack( true );
	}
	
	public void stepToParse( String path )
	{
		if ( null != path && !path.equals( Constants.consCancel ) )
		{
			mAquery.id( R.id.principal_button_begin_back ).enabled( false );
			mAquery.id( R.id.principal_progress_text_begin ).invisible().animate( R.anim.fadeout_short );
			mFileManager.beginParseTask( path );
		}
		else if ( null != path && path.equals( Constants.consCancel ) )
		{
			ToastHelper.showToast( this, R.string.cancel_info );
		}
		else
		{ 
			ToastHelper.showToast( this, R.string.error_begin );
			mAquery.id( R.id.principal_button_begin_back ).enabled( true ).click();
		}
	}
	

}
